package com.ssyt.examinationsystem.controller;

import com.alibaba.fastjson.JSON;
import com.ssyt.examinationsystem.dto.PaperDto;
import com.ssyt.examinationsystem.entity.CourseInfo;
import com.ssyt.examinationsystem.entity.PaperInfo;
import com.ssyt.examinationsystem.entity.RuleInfo;
import com.ssyt.examinationsystem.entity.TeacherInfo;
import com.ssyt.examinationsystem.service.IPaperInfoService;
import com.ssyt.examinationsystem.service.impl.PaperInfoServiceImpl;
import com.ssyt.examinationsystem.util.DateUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Servlet implementation class PaperInfoController
 */
public class PaperInfoController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public PaperInfoController() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 创建一个试卷对象
        IPaperInfoService paperService = new PaperInfoServiceImpl();

        PrintWriter out = response.getWriter();
        // 获取地址栏上的flag值
        String flag = request.getParameter("flag");
        if ("query".equals(flag)) {
            queryList(paperService, request, response, out);
        } else if ("list".equals(flag)) {
            list(request, response);
        } else if ("initAdd".equals(flag)) {
            initAdd(request, response);
        } else if ("scan".equals(flag)) {
            scan(request, response);
        } else if ("add".equals(flag)) {
            addPaper(paperService, request, response, out);
        } else if ("dele".equals(flag)) {
            deletePaper(paperService, request, response, out);
        } else if ("update".equals(flag)) {
            updatePaper(paperService, request, response, out);
        } else if ("scan_paper".equals(flag)) {
            scanPaper(paperService, request, response);
        }

        out.flush();
        out.close();
    }

    /**
     * 试卷预览
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void scanPaper(IPaperInfoService paperService, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String paperId = request.getParameter("paperId");
        //调用试卷数据传输业务方法
        PaperDto dto = paperService.getPaperInfoById(paperId);
        request.setAttribute("dto", dto);
        request.getRequestDispatcher("WEB-INF/views/paper/scanPaper.jsp").forward(request, response);

    }

    /**
     * 试卷显示
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void scan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.getRequestDispatcher("WEB-INF/views/paper/scan.jsp").forward(request, response);

    }

    /**
     * 转发到添加页面
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void initAdd(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.getRequestDispatcher("WEB-INF/views/paper/paperAdd.jsp").forward(request, response);
    }

    /**
     * 修改试卷的方法
     *
     * @param paperService
     * @param request
     * @param response
     * @param out
     */
    private void updatePaper(IPaperInfoService paperService, HttpServletRequest request, HttpServletResponse response,
                             PrintWriter out) {
        // 接收表单数据
        String paperId = request.getParameter("paperId");
        String paperName = request.getParameter("paperName");
        String courseId = request.getParameter("courseId");
        CourseInfo course = new CourseInfo();
        course.setCourseId(Integer.valueOf(courseId));

        String examTime = request.getParameter("examTime");
        String teaId = request.getParameter("teaId");
        TeacherInfo tea = new TeacherInfo();
        tea.setTeaId(Integer.valueOf(teaId));

        String paperDateString = request.getParameter("paperDate");
        Date paperDate = DateUtil.convertStringToDate(paperDateString);
        String passScore = request.getParameter("passScore");
        String ruleId = request.getParameter("ruleId");
        RuleInfo rule = new RuleInfo();
        rule.setRuleId(Integer.valueOf(ruleId));

        String testId = request.getParameter("testId");
        // 创建一个试卷对象，并封装属性
        PaperInfo paper = new PaperInfo();
        paper.setPaperName(paperName);
        paper.setCourse(course);
        paper.setExamTime(Integer.valueOf(examTime));
        paper.setTea(tea);
        paper.setPaperDate(paperDate);
        paper.setPassScore(passScore);
        paper.setRule(rule);
        paper.setTestId(Integer.valueOf(testId));
        paper.setPaperId(Integer.valueOf(paperId));

        if (paperService.updatePaper(paper) > 0) {
            out.print("ok");
        }
    }

    /**
     * 删除试卷的方法
     *
     * @param paperService
     * @param request
     * @param response
     * @param out
     */
    private void deletePaper(IPaperInfoService paperService, HttpServletRequest request, HttpServletResponse response,
                             PrintWriter out) {
        // 获取试卷编号
        String paperId = request.getParameter("paperId");

        if (paperService.deletePaper(paperId) > 0) {
            out.print("ok");
        }
    }

    /**
     * 添加试卷的方法
     *
     * @param paperService
     * @param request
     * @param response
     * @param out
     */
    private void addPaper(IPaperInfoService paperService, HttpServletRequest request, HttpServletResponse response,
                          PrintWriter out) {
        // 接收表单数据
        String paperName = request.getParameter("paperName");
        String courseId = request.getParameter("courseId");
        CourseInfo course = new CourseInfo();
        course.setCourseId(Integer.valueOf(courseId));

        String examTime = request.getParameter("examTime");
        String teaId = request.getParameter("teaId");
        TeacherInfo tea = new TeacherInfo();
        tea.setTeaId(Integer.valueOf(teaId));

        String paperDateString = request.getParameter("paperDate");
        Date paperDate = DateUtil.convertStringToDate(paperDateString);
        String passScore = request.getParameter("passScore");
        String ruleId = request.getParameter("ruleId");
        RuleInfo rule = new RuleInfo();
        rule.setRuleId(Integer.valueOf(ruleId));

        String testId = request.getParameter("testId");
        // 创建一个试卷对象，并封装属性
        PaperInfo paper = new PaperInfo();
        paper.setPaperName(paperName);
        paper.setCourse(course);
        paper.setExamTime(Integer.valueOf(examTime));
        paper.setTea(tea);
        paper.setPaperDate(paperDate);
        paper.setPassScore(passScore);
        paper.setRule(rule);
        paper.setTestId(Integer.valueOf(testId));

        // 添加试卷的业务方法
        int row = paperService.addPaper(paper);
        if (row > 0) {
            out.print("ok");
        }
    }

    /**
     * 转发到显示页面
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("WEB-INF/views/paper/paperList.jsp").forward(request, response);
    }

    /**
     * 显示所有试卷信息
     *
     * @param paperService
     * @param request
     * @param response
     * @param out
     */
    private void queryList(IPaperInfoService paperService, HttpServletRequest request, HttpServletResponse response,
                           PrintWriter out) {
        // 调用查询所有试卷信息的业务方法
        List<Map<String, Object>> paperList = paperService.getPapers();
        String paperJson = JSON.toJSONString(paperList, true);
        out.print(paperJson);
    }

}
